package com.pega.rasmus.testprime;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class testprime extends Activity {
	static final String TAG = "rasmus";	// for trace log
	protected static final int GUINOTIFIER = 0xb001cafe;
	
	private Button mButton;
	private EditText mEditText;
	private TextView mTextView;
	private ProgressDialog myDialog = null;
	protected Handler mHandler;
	private String queryResult = "";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Log.v(TAG, "onCreate");
        
		// get widgets
		mButton = (Button)findViewById(R.id.myButton);
		mEditText = (EditText)findViewById(R.id.myEditText);
		mTextView = (TextView)findViewById(R.id.myTextView);

		// set click listener to button
		mButton.setOnClickListener( myButtonListener );

		// handler for textview
		mHandler = new Handler()  {
			@Override
			public void handleMessage(Message msg)  {

				switch (msg.what)
				{
				case GUINOTIFIER:
					mTextView.setText(queryResult);
					break;
				}
				super.handleMessage(msg);
			}
		};	// handler
    		
    }
/*  
    @Override
    protected void onStart()  {
    	super.onStart();
    	Log.v(TAG, "onStart");
    }
*/
/*    
    @Override
    protected void onPause()  {
    	super.onPause();
    	Log.v(TAG, "onPause");
    	//mEditText.selectAll();
    }
*/    
/*    
    @Override
    protected void onResume()  {
    	super.onResume();
    	Log.v(TAG, "onResume");
    }
*/
/*   
    @Override
    protected void onRestart()  {
    	super.onRestart();
    	Log.v(TAG, "onRestart");
    }
*/
    
    // button to invoke query
	Button.OnClickListener myButtonListener = new OnClickListener()  {
		public void onClick(View v)  {
			mTextView.setText("");	// clear the textview
			String body = getString(R.string.STR_DLG_PLEASE_WAIT);
			String title = getString(R.string.STR_APP_NAME);
			
			// TODO start a thread to lookup the dictionary
      		myDialog = ProgressDialog.show(testprime.this, title, body, false);
			//Toast.makeText(ydict.this, body, Toast.LENGTH_LONG).show();
			
      		mEditText.selectAll();
      		
      		// thread to perform query action
      		new Thread(new Runnable()  {
				public void run()  {
					try  {
						String str = mEditText.getText().toString();
						int value = Integer.valueOf(str);
						
						// to query the ''str'' via ydict
						Log.v(TAG, "ask testIfPrime.isPrime(" + str + ")");
						boolean ret = testIfPrime.isPrime(value);
						
						queryResult = str;
						if (ret == true)  {
							queryResult += " is a prime!";
						}
						else  {
							queryResult += " is not a prime!";
						}
							
						Log.v(TAG, "send a message to handler");
						/* send message to handler after retrieve the query */
						Message m = new Message();
						m.what = GUINOTIFIER;
						mHandler.sendMessage(m);
					}
					catch (Exception e)  {
						e.printStackTrace();
						Log.e(TAG, "unhandled exceptions");
					}
					finally  {
						myDialog.dismiss();
					}
				}	// run()
			}).start();
		}	// onClick()
	};	// new OnClickListener ends


}	// end of activity testprime


class testIfPrime  {
	
	static int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 
						31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 
						73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
						127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
					};

	static int max_test = primes[primes.length-1] * primes[primes.length-1];
		
	public static boolean isPrime(int value) {
		
		if (value <= 1)
			return false;

		// test primes from preset table 
		for (int m : primes)  {
			if (value == m)
				return true;
			
			if (value % m == 0)  {
				//System.out.println("by: " + m);
				return false;
			}
		}

		// 2nd step to test prime
		//System.out.println(value + " -> 2nd step");
		int n = primes[primes.length-1] + 1;
		for ( ; n * n < value; ++n )  {
			if (value % n == 0)  {
				//System.out.println("by: " + n);
				return false;
			}
		}
		//System.out.println("last n: " + n);
		
		return true;
	}
	
}
